package com.yzyz.web.controller.visitor;

import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import com.yzyz.bean.scenic.PictureBean;
import com.yzyz.bean.scenic.ScenicBean;
import com.yzyz.data.CacheFactory;
import com.yzyz.service.PictureService;

/**
 * 
 * @author David Sun
 * @Date   2010-4-11
 *
 */
public class PictureController extends MultiActionController {
	private PictureService pictureService;
	
	
	public void setPictureService(PictureService pictureService) {
		this.pictureService = pictureService;
	}

	/**
	 * 
	 * @param request
	 * @param response
	 * @throws IOException 
	 */
	public void getPicture(HttpServletRequest request, HttpServletResponse response) throws IOException{
		int pictureId = Integer.valueOf(request.getParameter("pictureId"));
		PictureBean picture = (PictureBean)this.pictureService.find(pictureId);
		byte[] image = this.pictureService.resize(this.pictureService.bytes2Image(picture.getContents()), 120, 90, "jpg");
		response.setContentType("image/jpeg");
		response.setHeader("Content-Disposition", "attachment; filename=" + picture.getName());//设置图片的名字
		response.setContentLength(image.length);
		OutputStream out = response.getOutputStream();
		out.write(image);
		out.flush();
		out.close();
	}
	
	/**
	 * 
	 * @param request
	 * @param response
	 * @throws IOException 
	 */
	public void viewPicture(HttpServletRequest request, HttpServletResponse response) throws IOException{
		int pictureId = Integer.valueOf(request.getParameter("pictureId"));
		PictureBean picture = (PictureBean)this.pictureService.find(pictureId);
		byte[] image = this.pictureService.resize(this.pictureService.bytes2Image(picture.getContents()), 600, 450, "jpg");
		response.setContentType("image/jpeg");
		response.setHeader("Content-Disposition", "attachment; filename=" + picture.getName());//设置图片的名字
		response.setContentLength(image.length);
		OutputStream out = response.getOutputStream();
		out.write(image);
		out.flush();
		out.close();
	}
	
	/**
	 * 除了内容
	 * @param request
	 * @param response
	 * @return
	 */
	public ModelAndView getPictures(HttpServletRequest request, HttpServletResponse response){
		int scenicId 				= Integer.valueOf(request.getParameter("scenicId"));
		ScenicBean scenic 			= CacheFactory.getRegionStore().getScenicById(scenicId);
		List<PictureBean> pictures 	= scenic.getPictures();
		ModelAndView mv = new ModelAndView("/Scenic/Picture/picture.jsp");
		mv.addObject("pictures", pictures);
		mv.addObject("scenic", scenic);
		return mv;
	}
}
